package com.shopoo.oauth.domain.service;

import javax.annotation.Resource;

import com.shopoo.oauth.domain.entity.Menu;
import com.shopoo.oauth.domain.repository.MenuRepository;

import org.springframework.stereotype.Component;

/**
 *
 * @Author <a href="mailto:android_li@sina.cn">MaoYuan.Li</a>
 * @Date 2022/3/16 1:20 AM
 */
@Component
public class MenuDomainService {

	@Resource
	private MenuRepository menuRepository;

	/**
	 * 更新菜单信息
	 * @param menu
	 * @Return void
	 * @Date: 2022/3/17 1:50 AM
	 * @Author: <a href="mailto:android_li@sina.cn">MaoYuan.Li</a>
	 */ 
	public void updateById(Menu menu) {
		Long parentId = menu.getParentId();
		if (parentId.equals(0)) {
			menu.setLevel((short)1);
		} else {
			Menu tempOauthMenu = menuRepository.findById(menu.getParentId());
			if (tempOauthMenu == null) {
				throw new IllegalArgumentException("非法的父菜单ID："+parentId);
			}
			menu.setLevel((short)(tempOauthMenu.getLevel() + 1));
		}
		menuRepository.updateById(menu);
	}
}
